﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WMS.HelperClasses;
using WHMS.HelperClasses;
using Microsoft.Reporting.WinForms;
using SD.LLBLGen.Pro.ORMSupportClasses;
using WHMS.EntityClasses;

namespace WMS.Reports
{	
	public partial class ItemCardReportForm : Form
	{
        public int selectedStoreID;
        public int selectedProductID;

        public ItemCardReportForm()
		{
			InitializeComponent();
		}

		private void ItemBalanceForm_Load(object sender, EventArgs e)
		{
            //FormsHelper.LoadComboBox(StoresDDL, StoreFields.Id, StoreFields.Name, (x, y) => DALHelper.GetDynamicList(x, y, null));
            //FormsHelper.LoadComboBox(ItemsDDL, ItemViewFields.Id, ItemViewFields.NameCodeUnit, () => Business.GetItemsView(null));

			//this.reportViewer1.RefreshReport();

			new FormsHelper().DisableReportExportToWord(this.reportViewer1);
		}

		private void RunBtn_Click(object sender, EventArgs e)
		{
			//bindingSource1.DataSource = Business.GetItemCard(GetSelectedItemId(), GetSelectedStoreId(), FromDateDTP.Value, ToDateDTP.Value);
            bindingSource1.DataSource = Business.GetItemCard(selectedProductID, selectedStoreID, FromDateDTP.Value, ToDateDTP.Value);

			this.reportViewer1.LocalReport.DataSources.Clear();
			this.reportViewer1.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", bindingSource1));
			this.reportViewer1.LocalReport.SetParameters(new ReportParameter("StoreName", lblStoreName.Text));
			this.reportViewer1.LocalReport.SetParameters(new ReportParameter("ItemName", lblProductName.Text.Replace('(', ' ').Replace(')', ' ')));
			this.reportViewer1.LocalReport.SetParameters(new ReportParameter("FromDate", FromDateDTP.Value.ToShortDateString()));
			this.reportViewer1.LocalReport.SetParameters(new ReportParameter("ToDate", ToDateDTP.Value.ToShortDateString()));
		    this.reportViewer1.LocalReport.SetParameters(new ReportParameter("ItemCode", txtProductCode.Text));
		    this.reportViewer1.LocalReport.SetParameters(new ReportParameter("WarehouseCode", txtStoreCode.Text));

			this.reportViewer1.RefreshReport();   

		}

        //private int GetSelectedItemId()
        //{
        //    return (int)((System.Data.DataRowView)(ItemsDDL.SelectedItem)).Row.ItemArray[0];
        //}

        //private int GetSelectedStoreId()
        //{
        //    return (int)((System.Data.DataRowView)(StoresDDL.SelectedItem)).Row.ItemArray[0];
        //}

        //private string GetSelectedStoreName()
        //{
        //    return (string)((System.Data.DataRowView)(StoresDDL.SelectedItem)).Row.ItemArray[1];
        //}
        //private string GetSelectedItemName()
        //{
        //    return (string)((System.Data.DataRowView)(ItemsDDL.SelectedItem)).Row.ItemArray[1];
        //}

        private void txtStoreCode_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtStoreCode.Text))
            {
                DataTable DTStore = Business.GetStoreDetails(txtStoreCode.Text);
                if (DTStore.Rows.Count > 0)
                {
                    selectedStoreID = Int32.Parse(DTStore.Rows[0][0].ToString());
                    lblStoreName.Text = DTStore.Rows[0][2].ToString();
                    RunBtn.Enabled = true;
                }
                else
                {
                    lblStoreName.Text = string.Empty;
                    RunBtn.Enabled = false;
                }
            }
        }

        private void txtProductCode_TextChanged(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtProductCode.Text))
            {
                DataTable DTProduct = Business.GetItemDetails(txtProductCode.Text);
                if (DTProduct.Rows.Count > 0)
                {
                    selectedProductID = Int32.Parse(DTProduct.Rows[0][0].ToString());
                    lblProductName.Text = DTProduct.Rows[0][1].ToString();
                    RunBtn.Enabled = true;
                }
                else
                {
                    lblProductName.Text = string.Empty;
                    RunBtn.Enabled = false;
                }
            }
        }

        private void btnSearchStore_Click(object sender, EventArgs e)
        {
            
            var searchForm = new fromGeneralEntityPicker("Store");
            if (searchForm.ShowDialog() == DialogResult.OK)
            {
                txtStoreCode.Text = searchForm.SelectedItemCode;
                
            }
        }

        private void btnItemSearch_Click(object sender, EventArgs e)
        {
            
            var searchForm = new fromGeneralEntityPicker("Item");
            if (searchForm.ShowDialog() == DialogResult.OK)
            {
                txtProductCode.Text = searchForm.SelectedItemCode;

            }
        }

	}
}
